﻿package com.victim.snake.graphics
{
	import flash.display.Sprite;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import flash.events.TextEvent;
	import flash.text.TextFormat;
	import fl.controls.ComboBox;
	import com.victim.snake.util.Loader;
	import flash.system.IME;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import fl.data.DataProvider;
	import flash.events.MouseEvent;
	import com.victim.snake.user.User;
	import com.victim.snake.user.ProcessMsg;
	import flash.events.KeyboardEvent;
	import fl.controls.ScrollPolicy;
	import flash.events.FocusEvent;
	import flash.system.Capabilities;
	import fl.controls.ScrollBar;
	import fl.events.ScrollEvent;


	//聊天面版
	public class ChatSprite extends Sprite
	{
		protected var outputTextArea:TextArea=new TextArea();
		protected var inputText:TextInput=new TextInput();
		public var comboBox:ComboBox=new ComboBox();
		protected var sendButUpSkinLoader:flash.display.Loader=null;
		protected var sendButOverSkinLoader:flash.display.Loader=null;
		protected var w:int = 0;	//输出框宽
		protected var dataProvider:DataProvider=new DataProvider();
		protected var row:int=0;	//文本输出框行数
		protected var outputTextPattern:RegExp=/^<P.*?<\/P>/;
		public var scrollBar:ScrollBar=null;	//输出框滚动条
		
		public function ChatSprite(w:int):void
		{
			this.w = w;
			
			//聊天记录框文字样式
			var textAreaForma:TextFormat=new TextFormat();
			textAreaForma.size=12;

			//聊天记录框
			//this.textArea.htmlText="<p><a href='event:aa'><font color='#0000ff'>[aa]</font></a></p><p>aa1w1w中fwefewfewfwefweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee</p>";
			this.outputTextArea.verticalScrollPolicy=ScrollPolicy.OFF;
			this.outputTextArea.editable = false;
			this.outputTextArea.focusEnabled=false;
			this.outputTextArea.mouseEnabled=false;
			this.outputTextArea.mouseFocusEnabled=false;
			this.outputTextArea.drawFocus(false);
			this.outputTextArea.width = w;
			this.outputTextArea.height = 180;
			this.outputTextArea.setStyle("textFormat",textAreaForma);
			this.outputTextArea.addEventListener(TextEvent.LINK,talkTo);
			

			//输出框滚动条
			this.scrollBar=new ScrollBar();
			this.scrollBar.width=14;
			this.scrollBar.x=this.outputTextArea.x+this.outputTextArea.width+10;
			this.scrollBar.y=this.outputTextArea.y;
			this.scrollBar.height=this.outputTextArea.height;
			
			this.scrollBar.addEventListener(fl.events.ScrollEvent.SCROLL,mainChatScrollEvent);			




			//发送用户选择框
			this.comboBox.x = 0;
			this.comboBox.y = this.outputTextArea.height + 6;
			this.comboBox.width = 60;
			this.comboBox.editable = false;
			this.dataProvider=new DataProvider();
			this.dataProvider.addItem({label:"房间",data:"room"});
			this.dataProvider.addItem({label:"世界",data:"all"});
			this.comboBox.dataProvider = this.dataProvider;


			//发送图标
			new Loader("image/room/sign_in.png",this.sendButUpSkinLoaderEvent);
			new Loader("image/room/signInOver.png",this.sendButOverSkinLoaderEvent);

			//输入框
			this.inputText.y = this.outputTextArea.height + 8;
			this.inputText.x = this.comboBox.width + 2;
			this.inputText.width = w - comboBox.width - 2 - 18;
			this.inputText.height = 18;
			this.inputText.addEventListener(FocusEvent.FOCUS_IN,focusInHandler);
			this.inputText.addEventListener(KeyboardEvent.KEY_DOWN,enterKeyDown);
			this.inputText.addEventListener(MouseEvent.CLICK,inputTextMouseClick);
			
			this.addChild(outputTextArea);
			this.addChild(inputText);
			this.addChild(comboBox);
			this.addChild(this.scrollBar);
		}
		
		//自定滚动条滚动事件
		public function mainChatScrollEvent(e:ScrollEvent):void{
			this.outputTextArea.verticalScrollBar.scrollPosition=e.position;
		}

		//输入框取鼠标单击事件
		function inputTextMouseClick(e:MouseEvent):void{
			this.outputTextArea.verticalScrollPosition=this.outputTextArea.maxVerticalScrollPosition;
		}

		//回车事件
		function enterKeyDown(event:KeyboardEvent):void{
			if(event.keyCode==13){
				this.sendEvent(null);
			}
		}


		//玩家名称单击事件
		public function talkTo(event:TextEvent):void
		{

			for (var i:int=0; i<this.dataProvider.length; i++)
			{
				var o:Object = this.dataProvider.getItemAt(i);
				if (o["label"] == event.text)
				{
					this.comboBox.selectedItem = o;
					return;
				}
			}

			var obj:Object = {label:event.text,data:event.text};
			this.dataProvider.addItem(obj);
			this.comboBox.selectedItem = obj;

		}

		//发送按扭加载事件
		public function sendButUpSkinLoaderEvent(event:Event):void
		{
			sendButUpSkinLoader = LoaderInfo(event.target).loader;
			sendButUpSkinLoader.width = 18;
			sendButUpSkinLoader.height = 18;
			sendButUpSkinLoader.x = w - 18 + 2;
			sendButUpSkinLoader.y = this.outputTextArea.height + 8;

			sendButUpSkinLoader.addEventListener(MouseEvent.CLICK,sendEvent);
			sendButUpSkinLoader.addEventListener(MouseEvent.MOUSE_OVER,sendButUpSkinLoaderMouseOver);
			
			
			this.addChild(sendButUpSkinLoader);

		}
		
		//发送按扭加载事件
		public function sendButOverSkinLoaderEvent(event:Event):void
		{
			sendButOverSkinLoader = LoaderInfo(event.target).loader;
			sendButOverSkinLoader.width = 18;
			sendButOverSkinLoader.height = 18;
			sendButOverSkinLoader.x = w - 18 + 2;
			sendButOverSkinLoader.y = this.outputTextArea.height + 8;

			sendButOverSkinLoader.addEventListener(MouseEvent.CLICK,sendEvent);
			sendButOverSkinLoader.addEventListener(MouseEvent.MOUSE_OUT,sendButUpSkinLoaderMouseOut);
			//this.addChild(sendButOverSkinLoader);

		}
		
		public function sendButUpSkinLoaderMouseOver(e:Event):void{
			if(this.sendButOverSkinLoader!=null){
				this.addChild(this.sendButOverSkinLoader);
			}
		}
		
		public function sendButUpSkinLoaderMouseOut(e:Event):void{			
			this.addChild(this.sendButUpSkinLoader);			
		}

		//发送文字事件
		public function sendEvent(event:MouseEvent):void
		{
			if (this.inputText.text != "")
			{
				ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(14,this.comboBox.selectedItem["data"]+";"+this.inputText.text));
			}
			this.inputText.text="";
		}
		
		//添加聊天消息
		public function addTalkMsg(talkUser:String,talkContent:String):void{
			row+=1;
			var ht:String=this.outputTextArea.htmlText;
			ht+="<p><a href='event:" + talkUser + "'><font color='#0000ff'>[" + talkUser + "]</font></a>:"+talkContent+"</p>";
			if(row>20){
				ht=ht.replace(this.outputTextPattern,"");
			}
			
			this.outputTextArea.htmlText =  ht
			this.outputTextArea.verticalScrollPosition=this.outputTextArea.maxVerticalScrollPosition;
			
		}
		
		//textinput输入中文
		function focusInHandler(e:FocusEvent):void 
		{
			
			IME.enabled = true;
			
		}

	}

}